﻿using System;
using FirebirdSql.Data.FirebirdClient;
using Csla;
using System.Collections;
using System.Collections.Generic;
using System.Data;

namespace RMSM
{
    [Serializable()]
    public class CTQuayPhimBOCollection : Csla.BusinessListBase<CTQuayPhimBOCollection, CTQuayPhimBO>
    {
        #region BindingList Overrides
        protected override object AddNewCore()
        {
            CTQuayPhimBO _CTQuayPhimBO = CTQuayPhimBO.NewChildCTQuayPhimBO();
            _CTQuayPhimBO.STT0 = (this.Count + 1).ToString();
            OnAddingNew(new System.ComponentModel.AddingNewEventArgs(_CTQuayPhimBO));

            Add(_CTQuayPhimBO);
            return _CTQuayPhimBO;
        }
        #endregion BindingList Overrides
        //==========================================================================

        #region Factory Methods
        private CTQuayPhimBOCollection()
        {
        }

        public static CTQuayPhimBOCollection NewCTQuayPhimBOCollection()
        {
            return new CTQuayPhimBOCollection();
        }

        public static CTQuayPhimBO NewCTQuayPhimBO()
        {
            return CTQuayPhimBO.NewChildCTQuayPhimBO();
        }

        public static CTQuayPhimBOCollection GetCTQuayPhimBOCollection()
        {
            return GetCTQuayPhimBOCollection(null, null, null, false);
        }

        public static CTQuayPhimBOCollection GetCTQuayPhimBOCollection(bool getDeleted)
        {
            return GetCTQuayPhimBOCollection(null, null, null, getDeleted);
        }

        public static CTQuayPhimBOCollection GetCTQuayPhimBOCollection(string where)
        {
            return GetCTQuayPhimBOCollection(where, null, null, false);
        }

        public static CTQuayPhimBOCollection GetCTQuayPhimBOCollection(string where, bool getDeleted)
        {
            return GetCTQuayPhimBOCollection(where, null, null, getDeleted);
        }

        public static CTQuayPhimBOCollection GetCTQuayPhimBOCollection(string where, string orderCol)
        {
            return GetCTQuayPhimBOCollection(where, orderCol, null, false);
        }

        public static CTQuayPhimBOCollection GetCTQuayPhimBOCollection(string where, string orderCol, bool getDeleted)
        {
            return GetCTQuayPhimBOCollection(where, orderCol, null, getDeleted);
        }

        public static CTQuayPhimBOCollection GetCTQuayPhimBOCollection(string where, string orderCol, string orderExp)
        {
            return GetCTQuayPhimBOCollection(where, orderCol, orderExp, false);
        }

        public static CTQuayPhimBOCollection GetCTQuayPhimBOCollection(string where, string orderCol, string orderExp, bool getDeleted)
        {
            return GetCTQuayPhimBOCollection(where, orderCol, orderExp, getDeleted, false);
        }

        public static CTQuayPhimBOCollection GetCTQuayPhimBOCollection(string where, string orderCol, string orderExp, bool getDeleted, bool hasOtherParam)
        {
            string swhere = "";
            if (getDeleted == false)
                swhere += "WHERE DELE='0'" + (string.IsNullOrEmpty(where) ? "" : " AND " + where);
            else
                swhere = where;

            SPParam spParam = new SPParam((string.IsNullOrEmpty(orderCol) ? "CTQUAYPHIMID" : orderCol), (string.IsNullOrEmpty(orderExp) ? "ASC" : orderExp), swhere);
            if (hasOtherParam)
                spParam.objParam = "ABC";
            return DataPortal.Fetch<CTQuayPhimBOCollection>(spParam);
        }

        public static CTQuayPhimBOCollection GetCTQuayPhimBOCollection(int HopDongID)
        {
            SPParam spParam = new SPParam("LANQUAYPHIMID,CTQUAYPHIMID", "ASC", "WHERE DELE='0' AND HOPDONGID=" + HopDongID, true);
            spParam.otherParam = "HOPDONGID=" + HopDongID.ToString();
            return DataPortal.Fetch<CTQuayPhimBOCollection>(spParam);
        }
        #endregion Factory Methods
        //==========================================================================

        #region Criteria
        [Serializable()]
        private class Criteria
        {
            public Criteria()
            {
            }
        }

        [Serializable()]
        public class FilterCriteria
        {
            public FilterCriteria()
            {
            }
        }
        #endregion Criteria
        //==========================================================================

        #region Fetch
        [Transactional(TransactionalTypes.TransactionScope)]
        private void DataPortal_Fetch(SPParam spParam)
        {
            RaiseListChangedEvents = false;
            using (FbConnection conn = new FbConnection(Session.CnnStr))
            {
                conn.Open();
                using (FbCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "CTQuayPhimBO_C";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@WHEREEXP", FbDbType.VarChar).Value = spParam.whereExpression;
                    cmd.Parameters.Add("@ORDERCOL", FbDbType.VarChar).Value = spParam.orderColumn;
                    cmd.Parameters.Add("@ORDEREXP", FbDbType.VarChar).Value = spParam.orderExpression;

                    using (FbDataReader reader = cmd.ExecuteReader())
                    {
                        QuayPhimBO quayPhimBO = null;

                        while (reader.Read())
                        {
                            CTQuayPhimBO cTQuayPhimBO = CTQuayPhimBO.GetChildCTQuayPhimBO(reader);

                            if (spParam.objParam != null)
                            {
                                cTQuayPhimBO.StrSoTien = StaticUtil.GetSoTienStr(cTQuayPhimBO.SoTien);
                                cTQuayPhimBO.StrDiscount = StaticUtil.GetSoTienStr(cTQuayPhimBO.Discount);
                                cTQuayPhimBO.StrThanhTien = StaticUtil.GetSoTienStr(cTQuayPhimBO.ThanhTien);
                                cTQuayPhimBO.StrSoLuong = StaticUtil.GetIntStr(cTQuayPhimBO.SoLuong);
                                cTQuayPhimBO.StrEditOne = cTQuayPhimBO.CTQuayPhimID.Value.ToString();
                                cTQuayPhimBO.StrEditAll = cTQuayPhimBO.LanQuayPhimID.Value.ToString();

                                if (cTQuayPhimBO.StrDiscount != "")
                                    cTQuayPhimBO.StrDiscount += " %";

                                quayPhimBO = Session.quayPhimBOCollection.GetBO(cTQuayPhimBO.QuayPhimID);
                                if (quayPhimBO != null)
                                {
                                    cTQuayPhimBO.StrQuayPhimID = quayPhimBO.Ma;
                                    cTQuayPhimBO.StrQuayPhimIDTen = quayPhimBO.Ten;
                                }
                            }

                            this.Add(cTQuayPhimBO);
                        }
                    }
                }
            }
            RaiseListChangedEvents = true;
        }
        #endregion Fetch
        //==========================================================================

        #region Update
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Update()
        {
            Update();
        }

        internal void Update()
        {
            RaiseListChangedEvents = false;
            CTQuayPhimBO child = null;
            int length = this.Count;
            bool del = false;
            int index = -1;
            for (int i = 0; i < length; i++)
            {
                child = this[i];
                del = StaticUtil.GetBool(child.DELE);

                if (child.IsNew && !del)
                {
                    child.Insert();
                    index = Session.quayPhimBOCollection.GetIndex(child.QuayPhimID);
                    if (index >= 0)
                        Session.quayPhimBOCollection[index].LanSuDung = StaticUtil.GetInt(Session.quayPhimBOCollection[index].LanSuDung) + 1;
                }
                else
                {
                    child.Update();
                }

                if (del)
                {
                    index = Session.quayPhimBOCollection.GetIndex(child.QuayPhimID);
                    if (index >= 0)
                        Session.quayPhimBOCollection[index].LanSuDung = StaticUtil.GetInt(Session.quayPhimBOCollection[index].LanSuDung) - 1;

                    this.RemoveAt(i);
                    i--;
                    length--;
                }
            }

            Session.quayPhimBOCollection = Session.quayPhimBOCollection.Save();
            RaiseListChangedEvents = true;
        }
        #endregion Update
        //==========================================================================

        #region Business Method
        public CTQuayPhimBO GetBO(int? CTQuayPhimID)
        {
            if (CTQuayPhimID == null)
                return null;

            int length = this.Count;
            for (int i = 0; i < length; i++)
                if (this[i].CTQuayPhimID == CTQuayPhimID)
                    return this[i];

            return null;
        }

        public CTQuayPhimBOCollection GetCTQuayPhimBOCollectionByLanQuayPhimID(int LanQuayPhimID)
        {
            CTQuayPhimBOCollection ctQuayPhimBOCollection = CTQuayPhimBOCollection.NewCTQuayPhimBOCollection();
            int length = this.Count;
            bool flag = false;
            for (int i = 0; i < length; i++)
            {
                if (this[i].LanQuayPhimID.Value == LanQuayPhimID)
                {
                    flag = true;
                    ctQuayPhimBOCollection.Add(this[i].Clone());
                }
                else
                    if (flag)
                        break;
            }

            return ctQuayPhimBOCollection;
        }

        public int GetIndex(int? CTQuayPhimID)
        {
            if (CTQuayPhimID == null)
                return -1;

            int length = this.Count;
            for (int i = 0; i < length; i++)
                if (this[i].CTQuayPhimID == CTQuayPhimID)
                    return i;

            return -1;
        }

        public void Remove(int? CTQuayPhimID)
        {
            if (CTQuayPhimID == null)
                return;

            int length = this.Count;
            for (int i = 0; i < length; i++)
            {
                if (this[i].CTQuayPhimID == CTQuayPhimID)
                {
                    try
                    {
                        this.RemoveAt(i);
                    }
                    catch { }

                    break;
                }
            }
        }
        #endregion Business Method
        //==========================================================================
    }
}